package com.databaseprotect;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import org.w3c.dom.Text;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    public boolean User_Status=false;
    public static String m_username="";
    public static String m_password=null;
    public static String m_user_type="user";
    public static boolean status=false;
    List<Commodity> lists=new ArrayList<>();
    ListView mListView;
    ListView type_ListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //沉浸式状态栏
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            try {
                Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);;
                field.setInt(getWindow().getDecorView(), Color.TRANSPARENT);
            }catch(Exception e){
            }
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        mListView=findViewById(R.id.main_list);
        type_ListView=findViewById(R.id.type_list);
        TextView sum = (TextView) findViewById(R.id.true_sum);
        //获取数据库信息写入对象数组
        new Thread(new Runnable() {
            @Override
            public void run() {
                Connection conn = null;
                conn = (Connection) DBHelper.getConn();
                Statement statement=null;
                try {
                    statement=conn.createStatement();
                    String sql="select * from meal;";
                    ResultSet resultSet=statement.executeQuery(sql);
                    while(resultSet.next()) {
                            Commodity commodity = new Commodity();
                            commodity.name = resultSet.getString("name");
                            commodity.introduce = resultSet.getString("introduce");
                            commodity.price = resultSet.getString("price");
                            commodity.img_url = resultSet.getString("img_src");
                            commodity.num = resultSet.getString("num");
                            lists.add(commodity);
                    }
                    conn.close();
                    statement.close();
                    resultSet.close();
                    Message message = new Message();
                    message.what = 0;
                    handler.sendMessage(message);
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }).start();
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ImageView onclick=(ImageView)view.findViewById(R.id.add);
                onclick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView u_num = (TextView) view.findViewById(R.id.u_num);
                        TextView disp_sum = (TextView) findViewById(R.id.sum);
                        TextView price = (TextView) view.findViewById(R.id.price);
                        TextView cnum = (TextView) view.findViewById(R.id.cnum);
                        ImageView minus = (ImageView) view.findViewById(R.id.minus);
                        int num = Integer.parseInt(cnum.getText().toString());
                        if (num + 1 > Integer.parseInt(u_num.getText().toString())) {
                            Toast.makeText(MainActivity.this, "已经是最大库存", Toast.LENGTH_SHORT).show();
                        } else {
                            num++;
                            double sum_price = Double.parseDouble(sum.getText().toString()) + Double.parseDouble(price.getText().toString());
                            sum.setText(""+sum_price);
                            disp_sum.setText("￥" + sum_price);
                            cnum.setText(Integer.toString(num));
                            cnum.setVisibility(View.VISIBLE);
                            minus.setVisibility(View.VISIBLE);
                        }
                    }
                });
                ImageView minus=(ImageView)view.findViewById(R.id.minus);
                minus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView disp_sum=(TextView)findViewById(R.id.sum);
                        TextView price=(TextView)view.findViewById(R.id.price);
                        TextView cnum=(TextView)view.findViewById(R.id.cnum);
                        ImageView minus=(ImageView)view.findViewById(R.id.minus);
                        int num=Integer.parseInt(cnum.getText().toString());
                        num--;
                        double sum_price = Double.parseDouble(sum.getText().toString()) - Double.parseDouble(price.getText().toString());
                        sum.setText(""+sum_price);
                        disp_sum.setText("￥"+sum_price);
                        cnum.setText(Integer.toString(num));
                        if(num<=0) {
                            cnum.setVisibility(View.GONE);
                            minus.setVisibility(View.GONE);
                        }
                    }
                });
            }
        });
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                mListView.setAdapter(new MyAdapter());
            }
        }
    };
    public class MyAdapter extends BaseAdapter{
        @Override
        public int getCount(){
            return lists.size();
        }
        @Override
        public Object getItem(int position){
            return null;
        }
        @Override
        public long getItemId(int position){
            return 0;
        }
        @Override
        public View getView(int position, View conterview, ViewGroup parent){
            Commodity commodity=lists.get(position);
            View view=View.inflate(MainActivity.this,R.layout.item,null);
            MyImageView img_src=view.findViewById(R.id.pay_img);
            String url="https://lemonscloud.cn/"+commodity.img_url;
            System.out.println(url);
            img_src.setImageURL(url);
            TextView name=view.findViewById(R.id.pay_name);
            name.setText(commodity.name);
            TextView introduce=view.findViewById(R.id.pay_introduce);
            introduce.setText(commodity.introduce);
            TextView price=view.findViewById(R.id.pay_price);
            price.setText("￥"+commodity.price);
            TextView u_price=view.findViewById(R.id.price);
            u_price.setText(commodity.price);
            TextView u_mid=view.findViewById(R.id.u_mid);
            u_mid.setText(commodity.mid);
            TextView u_num=(TextView)view.findViewById(R.id.u_num);
            u_num.setText(commodity.num);
            return view;
        }
    }
    public void touser(View v){
        Intent intent=new Intent(this,User.class);
        startActivity(intent);
    }
    public void put(View v){
        TextView sum = (TextView) findViewById(R.id.true_sum);
        if(m_username.equals("")||m_username.equals(null)){
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            finish();
            Intent intent=new Intent(this,Login.class);
            startActivity(intent);
        }else if(Double.parseDouble(sum.getText().toString())<=0){
            Toast.makeText(this, "请选择菜品", Toast.LENGTH_SHORT).show();
        }else{
            finish();
            Intent intent=new Intent(this,Success.class);
            startActivity(intent);
        }
    }
}